#ifndef __KMP_STRING_H__
#define __KMP_STRING_H__

#include "osa_common.h"

/**

	KMP string routine.

	Remember: the length of pattern should be less then 255

*/
typedef struct kmp_string_t
{
    const char *pattern;	// pattern to be matched
    uint8_t	   *prefix_tbl;	// prefix table for matching
    uint8_t		pattern_len;// length of pattern
    uint8_t		matched;	// already matched
} kmp_string_t;


#define DEFINE_KMP_PATTERN(name, pat)							\
	const char PAT_##name[] = pat;					\
	uint8_t PREFIX_##name[sizeof(PAT_##name)];

#define DEFINE_KMP(name)	\
{ PAT_##name, PREFIX_##name, sizeof(PAT_##name) - 1, 0 }

EXTERN_C_BLOCKSTART

    /**
    	Init prefix table in kmp_string
    */
    void kmp_init(kmp_string_t *s);

    /**
    	try to match char c in kmp_string s
    	return currently matched count of char
    */
    int  kmp_find(kmp_string_t *s, const char c);

    /**
        Reset matched count in s
    */
    void kmp_reset(kmp_string_t *s);

EXTERN_C_BLOCKEND
#endif /* __KMP_STRING_H__ */
